package qc.module.platform.api.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import qc.common.core.constants.QCAuthConstant;
import qc.common.core.enums.ResourceStatusFlagEnum;
import qc.common.core.exception.QCPromptException;
import qc.common.core.exception.QcTokenInvalidException;
import qc.common.core.tree.QCTreeNode;
import qc.common.core.tree.TreeDataDto;
import qc.common.core.unify.QCUnifyReturnValue;
import qc.module.platform.dto.dept.*;
import qc.module.platform.dto.project.ProjectDto;
import qc.module.platform.dto.user.UserQueryConditionDto;
import qc.module.platform.entity.*;
import qc.module.platform.service.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * DeptController
 *
 * @author QuCheng Tech
 * @since 2023/5/28
 */
@RestController
@RequestMapping("/dept")
public class DeptController {
    private DeptService deptService;
    private UserLoginLogService userLoginLogService;
    private ProjectService projectService;

    private UserService userService;

    private DeptUserService deptUserService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    public void setDeptUserService(DeptUserService deptUserService) {
        this.deptUserService = deptUserService;
    }

    @Autowired
    public void setDeptService(DeptService deptService) {
        this.deptService = deptService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setUserLoginLogService(UserLoginLogService userLoginLogService) {
        this.userLoginLogService = userLoginLogService;
    }

    @Autowired
    public void setProjectService(ProjectService projectService) {
        this.projectService = projectService;
    }

    //是否为逻辑删除，为true表示逻辑删除--修改记录标记，为false表示物理删除--从数据库表中删除记录
    private static final boolean isLogicDelete = false;
    //部门支持的最大层级数
    private static final Integer maxDeptLevel = 0x3;

    /**
     * 查询指定项目编码类的部门/组织机构信息，用于组织机构管理界面使用，返回所有状态的记录
     *
     * @param pcode 项目编码
     * @return 部门/组织机构集合
     * @author QcCheng Tech
     * @since 2023/5/28
     */
    @RequestMapping(value = "/listdetail", method = {RequestMethod.GET, RequestMethod.POST})
    public List<DeptDto> queryListDetail(@RequestParam String pcode) throws QCPromptException {
        return deptService.query(pcode, true, 0x0,false);
    }

    /**
     * 查询指定项目编码的部门/组织机构信息，返回可见和可用的，简要信息
     *
     * @param pcode 项目编码
     * @return 部门/组织机构集合
     * @author QcCheng Tech
     * @since 2023/5/28
     */
    @RequestMapping(value = "/listsimple", method = {RequestMethod.GET, RequestMethod.POST})
    public List<DeptSimpleDto> queryListSimple(@RequestParam String pcode) throws QCPromptException {
        return deptService.querySimpleInfoList(pcode, false, 0x0,false);
    }

    /**
     * 查询指定项目编码的部门/组织机构信息，返回可见和可用的；结果以TreeTable组件要求的数据格式返回各栏目的详细信息；
     *
     * @param pcode 项目编码
     * @return 部门/组织机构集合
     * @author QcCheng Tech
     * @since 2023/5/28
     */
    @RequestMapping(value = "/treetable", method = {RequestMethod.GET, RequestMethod.POST})
    public List<DeptTreeTableDetailDto> queryTreeTable(@RequestParam String pcode) throws QCPromptException {
        return deptService.queryTreeTableDetail(pcode, false, 0x3);
    }

    /**
     * 查询指定项目中的所有部门信息，以树形结构形式返回数据；
     *
     * @param pcode: 项目编码
     * @return List<QCTreeNode> 返回结果集
     * @author QcCheng Tech
     * @date 2023/9/6
     */
    @RequestMapping(value = "/gettree", method = {RequestMethod.GET, RequestMethod.POST})
    public List<QCTreeNode> getProjectDeptsTree(@RequestParam String pcode) throws QCPromptException {
        //按项目编码查询部门（包含禁用部门）
        List<DeptSimpleDto> deptSimpleDtos = deptService.querySimpleInfoList(pcode, true, 0, false);
        //处理返回结果
        return getDeptTreeData(deptSimpleDtos);
    }

    /**
     * 获取指定部门信息
     *
     * @param id 部门ID
     * @return 部门信息
     * @author QcCheng Tech
     * @since 2023/5/28
     */
    @RequestMapping(value = "/get", method = {RequestMethod.GET})
    public DeptDto getByParam(@RequestParam Integer id) throws QCPromptException {
        return deptService.get(id);
    }

    /**
     * 获取指定部门信息
     *
     * @param id 部门ID
     * @return 部门信息
     * @author QcCheng Tech
     * @since 2023/6/26
     */
    @RequestMapping(value = "/get/{id}", method = {RequestMethod.GET})
    public DeptDto getByPath(@PathVariable Integer id) throws QCPromptException {
        return deptService.get(id);
    }

    /**
     * 新增部门
     *
     * @param dto 部门
     * @return 成功返回null
     * @author QcCheng Tech
     * @since 2023/5/28
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    public String add(@RequestBody DeptDto dto) {
        return deptService.add(dto, maxDeptLevel);
    }

    /**
     * 修改部门
     *
     * @param dto 部门
     * @return 成功返回null
     * @author QcCheng Tech
     * @since 2023/5/28
     */
    @RequestMapping(value = "/update", method = {RequestMethod.PUT, RequestMethod.POST})
    public String update(@RequestBody DeptDto dto) {
        return deptService.update(dto, maxDeptLevel);
    }

    /**
     * 删除部门，物理删除
     *
     * @param id 部门ID
     * @return 成功返回null
     * @author QcCheng Tech
     * @since 2023/5/28
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.DELETE})
    public String deleteByParam(@RequestParam Integer id) {
        return deptService.delete(id, isLogicDelete);
    }

    /**
     * 删除部门，物理删除
     *
     * @param id 部门ID
     * @return 成功返回null
     * @author QcCheng Tech
     * @since 2023/6/26
     */
    @RequestMapping(value = "/delete/{id}", method = {RequestMethod.DELETE})
    public String deleteByPath(@PathVariable Integer id) {
        return deptService.delete(id, isLogicDelete);
    }

    /**
     * 以树形结构返回结果，树形结构一级节点为部门，二级节点为部门或部门中的用户；
     *
     * @param id: 部门ID
     * @return TreeDataDto 返回结果集
     * @author QcCheng Tech
     * @date 2023/9/8
     */
    @RequestMapping(value = "/getuserstree", method = {RequestMethod.POST, RequestMethod.GET})
    public TreeDataDto getDeptUsersTreeByParam(@RequestParam int id) throws QCPromptException {
        TreeDataDto result = getDeptUsersTree(id, false);
        return result;
    }

    /**
     * 以树形结构返回用户结果
     *
     * @param id: 项目id
     * @return TreeDataDto 返回结果集
     * @author QcCheng Tech
     * @date 2023/9/8
     */
    @RequestMapping(value = "/getuserstree/{id}", method = {RequestMethod.POST, RequestMethod.GET})
    public TreeDataDto getDeptUsersTreeByPath(@PathVariable int id) throws QCPromptException {
        TreeDataDto result = getDeptUsersTree(id, false);
        return result;
    }

    /**
     * 设置部门和用户的关联关系
     *
     * @param dto: 关联关系
     * @return String 设置成功返回null,失败返回错误信息
     * @author QcCheng Tech
     * @date 2023/9/13
     */
    @RequestMapping(value = "/setusers", method = {RequestMethod.GET, RequestMethod.POST})
    public String setDeptUsers(@RequestBody DeptUserSetDto dto) {
        if (dto == null)
            return QCUnifyReturnValue.Warn("传入的设置条件为空");
        if (!deptService.hasIdExist(dto.getDeptid()))
            return QCUnifyReturnValue.Warn("指定的部门信息错误或不存在");

        //先按部门ID删除已有的关联
        deptUserService.deleteByDeptId(dto.getDeptid());
        //如果有设置关联用户ID信息，逐个进行添加
        if (dto.getUids() != null && dto.getUids().size() > 0x0) {
            for (int userid : dto.getUids()) {
                deptUserService.add(userid, dto.getDeptid());
            }
        }
        return QCUnifyReturnValue.Success();
    }

    /**
     * 获取平台中的默认（如果有多个项目则为第一个项目）项目中的所有有效部门
     *
     * @return 部门简要信息集合
     * @author QuCheng Tech
     * @since 2023/8/13
     */
    @GetMapping("/valid")
    public List<DeptSimpleDto> getDefaultProject(@RequestParam(value = "id", required = false) String id) throws QCPromptException {
        String projectCode = getProjectCodeByRequestParamOrDefault(id);

        if (!StringUtils.isBlank(projectCode))
            return deptService.querySimpleInfoList(projectCode, false, 0x0, false);

        return null;
    }

    /**
     * 获取平台中的默认（如果有多个项目则为第一个项目）项目中的所有有效部门
     *
     * @return 部门简要信息集合
     * @author QuCheng Tech
     * @since 2023/8/13
     */
    @GetMapping("/validtree")
    @Deprecated
    public List<QCTreeNode> getDefaultProjectValidTree(@RequestParam(value = "id", required = false) String id) throws QCPromptException {
        String projectCode = getProjectCodeByRequestParamOrDefault(id);

        if (!StringUtils.isBlank(projectCode)) {
            List<DeptSimpleDto> deptSimpleDtos = deptService.querySimpleInfoList(projectCode, false, 0x0, false);

            return getDeptTreeData(deptSimpleDtos);
        }

        return null;
    }

    /**
     * 获取指定部门的简要信息，返回树型结构
     * 从当前用户项目有效部门中获取
     *
     * @param deptid 部门ID
     * @return DeptTreeSimpleDto
     * @author QuCheng Tech
     * @since 2024/6/4
     */
    @RequestMapping(value = "/treesimplebydept", method = {RequestMethod.GET})
    public List<DeptTreeSimpleDto> getDeptTreeSimpleByParam(@RequestParam(required = false) Integer deptid) throws QCPromptException, QcTokenInvalidException {
        //项目编码的获取：先从请求头中获取、如果为空再根据token在登录日志中获取
        String projectCode = request.getHeader(QCAuthConstant.PROJECT_CODE_HEADER_KEY);
        //如果传入参数中项目编码为空，从请求头中获取当前项目编码
        if (StringUtils.isBlank(projectCode)) {
            //根据token获取项目编码
            String token = request.getHeader(QCAuthConstant.TOKEN_HEADER_KEY);
            //从uac中根据token获取项目编码
            QcUserloginLogs loginLog = userLoginLogService.getUserLoginLogByToken(token, true);

            if (StringUtils.isBlank(loginLog.getProject()))
                throw new QCPromptException("根据token获取到的用户信息中无项目编码信息");

            projectCode = loginLog.getProject();
        }

        return deptService.getDeptTreeSimple(projectCode, false, deptid);
    }

    /**
     * 获取指定部门的简要信息，返回树型结构
     * 从当前用户项目有效部门中获取
     *
     * @param deptid 部门ID
     * @return DeptTreeSimpleDto
     * @author QuCheng Tech
     * @since 2024/6/4
     */
    @RequestMapping(value = "/treesimplebydept/deptid", method = {RequestMethod.GET})
    public List<DeptTreeSimpleDto> getDeptTreeSimpleByPath(@PathVariable(required = false) Integer deptid) throws QCPromptException, QcTokenInvalidException {
        //项目编码的获取：先从请求头中获取、如果为空再根据token在登录日志中获取
        String projectCode = request.getHeader(QCAuthConstant.PROJECT_CODE_HEADER_KEY);
        //如果传入参数中项目编码为空，从请求头中获取当前项目编码
        if (StringUtils.isBlank(projectCode)) {
            //根据token获取项目编码
            String token = request.getHeader(QCAuthConstant.TOKEN_HEADER_KEY);
            //从uac中根据token获取项目编码
            QcUserloginLogs loginLog = userLoginLogService.getUserLoginLogByToken(token, true);

            if (StringUtils.isBlank(loginLog.getProject()))
                throw new QCPromptException("根据token获取到的用户信息中无项目编码信息");

            projectCode = loginLog.getProject();
        }

        return deptService.getDeptTreeSimple(projectCode, false, deptid);
    }

    /**
     * 获取部门负责人管理用户
     * 以树形结构返回结果
     *
     * @param id: 部门ID
     * @return TreeDataDto 返回结果集
     * @author QcCheng Tech
     * @date 2024/9/14
     */
    @RequestMapping(value = "/getleaderstree", method = {RequestMethod.POST, RequestMethod.GET})
    public TreeDataDto getDeptLeadersTreeByParam(@RequestParam int id) {
        TreeDataDto result = getDeptUsersTree(id, true);
        return result;
    }

    /**
     * 获取部门负责人管理用户
     * 以树形结构返回结果
     *
     * @param id: 项目id
     * @return TreeDataDto 返回结果集
     * @author QcCheng Tech
     * @date 2024/9/14
     */
    @RequestMapping(value = "/getleaderstree/{id}", method = {RequestMethod.POST, RequestMethod.GET})
    public TreeDataDto getDeptLeadersTreeByPath(@PathVariable int id) {
        TreeDataDto result = getDeptUsersTree(id, true);
        return result;
    }

    /**
     * 设置部门和用户的关联关系
     *
     * @param dto: 关联关系
     * @return String 设置成功返回null,失败返回错误信息
     * @author QcCheng Tech
     * @date 2023/9/14
     */
    @RequestMapping(value = "/setleaders", method = {RequestMethod.GET, RequestMethod.POST})
    public String setDeptLeaders(@RequestBody DeptUserSetDto dto) {
        if (dto == null)
            return QCUnifyReturnValue.Warn("传入的设置条件为空");
        if (!deptService.hasIdExist(dto.getDeptid()))
            return QCUnifyReturnValue.Warn("指定的部门信息错误或不存在");

        //先将指定部门关联的用户全部修改为不是部门负责人
        deptUserService.updateIsLeaderByDeptId(dto.getDeptid(), false, null);
        //然后将设置的用户修改为是部门负责人
        if (CollectionUtils.isNotEmpty(dto.getUids()))
            deptUserService.updateIsLeaderByDeptId(dto.getDeptid(), true, dto.getUids());

        return QCUnifyReturnValue.Success();
    }

    /**
     * 获取项目中的所有有效部门，可不指定项编码，如未传入项目编码从token中获取项目
     *
     * @param id 项目编码，可以为空
     * @return 部门简要信息集合
     * @author QuCheng Tech
     * @since 2023/5/28
     */
    @GetMapping("/projectvalid")
    public List<DeptSimpleDto> getProjectValid(@RequestParam(value = "id", required = false) String id) throws QCPromptException, QcTokenInvalidException {
        //项目编码的获取：先从传入参数中获取，如果为空再请求头中获取、如果为空再根据token在登录日志中获取
        String projectCode = id;
        if (StringUtils.isBlank(projectCode))
            projectCode = request.getHeader(QCAuthConstant.PROJECT_CODE_HEADER_KEY);
        //如果传入参数中项目编码为空，从请求头中获取当前项目编码
        if (StringUtils.isBlank(projectCode)) {
            //根据token获取项目编码
            String token = request.getHeader(QCAuthConstant.TOKEN_HEADER_KEY);
            //从uac中根据token获取项目编码
            QcUserloginLogs loginLog = userLoginLogService.getUserLoginLogByToken(token, true);

            if (StringUtils.isBlank(loginLog.getProject()))
                throw new QCPromptException("根据token获取到的用户信息中无项目编码信息");

            projectCode = loginLog.getProject();
        }

        return deptService.querySimpleInfoList(projectCode, false, 0x0,false);
    }

    /**
     * 列出指定项目下所有，只返回可用/可见状态；id参数必须传入项目编码，返回树形结构；
     * 该接口用于Platform中调用；如：项目用户管理中需要以树形结构显示项目中的所有部门，用于选择项目用户所属的部门；
     *
     * @param id 项目编码，必须传入
     * @return 部门简要信息集合
     * @author QuCheng Tech
     * @since 2023/5/28
     */
    @RequestMapping(value = "/projectvalidtree", method = {RequestMethod.GET})
    public List<QCTreeNode> getProjectValidTreeByParam(@RequestParam(value = "id", required = true) String id) throws QCPromptException {
        //按项目编码查询有效部门
        List<DeptSimpleDto> deptSimpleDtos = deptService.querySimpleInfoList(id, false, 0x0, false);
        //处理返回结果
        return getDeptTreeData(deptSimpleDtos);
    }

    /**
     * 列出指定项目下所有，只返回可用/可见状态；id参数必须传入项目编码，返回树形结构；
     * 该接口用于Platform中调用；如：项目用户管理中需要以树形结构显示项目中的所有部门，用于选择项目用户所属的部门；
     *
     * @param id 项目编码，必须传入
     * @return 部门简要信息集合
     * @author QuCheng Tech
     * @since 2023/5/28
     */
    @RequestMapping(value = "/projectvalidtree/{id}", method = {RequestMethod.GET})
    public List<QCTreeNode> getProjectValidTreeByPath(@PathVariable(value = "id", required = true) String id) throws QCPromptException {
        //按项目编码查询有效部门
        List<DeptSimpleDto> deptSimpleDtos = deptService.querySimpleInfoList(id, false, 0x0, false);
        //处理返回结果
        return getDeptTreeData(deptSimpleDtos);
    }

    /***
     * 从传入的参数中获取项目编码或获取默认项目编码
     *
     * @param requestParam 请求参数中的项目编码
     * @return java.lang.String
     * @author QuCheng Tech
     * @since 2023/9/6
     */
    String getProjectCodeByRequestParamOrDefault(String requestParam) {
        String projectCode = null;
        if (!StringUtils.isBlank(requestParam)) {
            QcProject project = projectService.getEntity(requestParam, true);
            if (project != null)
                projectCode = requestParam;
        }
        //如果没有传入项目编码或者传入的项目编码无效，获取系统中的默认一个有效项目编码
        if (StringUtils.isBlank(projectCode)) {
            List<ProjectDto> allValidProjects = projectService.getValidProjectsList(ResourceStatusFlagEnum.NORMAL);
            if (allValidProjects != null && allValidProjects.size() > 0x0) {
                projectCode = allValidProjects.get(0x0).getCode();
            }
        }

        return projectCode;
    }

    /***
     * 将部门信息集合对象转换为树形结构对象
     *
     * @param depts 部门信息集合
     * @return java.util.List<qc.common.core.tree.QCTreeNode>
     * @author QuCheng Tech
     * @since 2023/8/13
     */
    List<QCTreeNode> getDeptTreeData(List<DeptSimpleDto> depts) {
        if (depts != null && depts.size() > 0x0) {
            List<QCTreeNode> result = new ArrayList<>();

            //获取第一级部门
            List<DeptSimpleDto> level1Depts = depts.stream().filter(p -> p.getParentid() == 0x0).collect(Collectors.toList());
            if (level1Depts != null && level1Depts.size() > 0x0) {
                for (DeptSimpleDto level1Dept : level1Depts) {
                    QCTreeNode level1TreeNode = new QCTreeNode();
                    level1TreeNode.setKey(String.valueOf(level1Dept.getId()));
                    level1TreeNode.setTitle(level1Dept.getName());
                    level1TreeNode.setLeaf(true);

                    //获取一级部门的下级部门
                    List<DeptSimpleDto> level2Depts = depts.stream().filter(p -> p.getParentid() == level1Dept.getId()).collect(Collectors.toList());
                    if (level2Depts != null && level2Depts.size() > 0x0) {
                        level1TreeNode.setLeaf(false);
                        List<QCTreeNode> level2TreeNodes = new ArrayList<>();
                        for (DeptSimpleDto level2Dept : level2Depts) {
                            QCTreeNode level2TreeNode = new QCTreeNode();
                            level2TreeNode.setKey(String.valueOf(level2Dept.getId()));
                            level2TreeNode.setTitle(level2Dept.getName());
                            level2TreeNode.setLeaf(true);

                            //获取二级部门的下级部门
                            List<DeptSimpleDto> level3Depts = depts.stream().filter(p -> p.getParentid() == level2Dept.getId()).collect(Collectors.toList());
                            if (level3Depts != null && level3Depts.size() > 0x0) {
                                level2TreeNode.setLeaf(false);
                                List<QCTreeNode> level3TreeNodes = new ArrayList<>();
                                for (DeptSimpleDto level3Dept : level3Depts) {
                                    QCTreeNode level3TreeNode = new QCTreeNode();
                                    level3TreeNode.setKey(String.valueOf(level3Dept.getId()));
                                    level3TreeNode.setTitle(level3Dept.getName());
                                    level3TreeNode.setLeaf(true);
                                    //可以继续获取四级级节点

                                    level3TreeNodes.add(level3TreeNode);
                                }
                                level2TreeNode.setChildren(level3TreeNodes);
                            }
                            level2TreeNodes.add(level2TreeNode);
                        }
                        level1TreeNode.setChildren(level2TreeNodes);
                    }
                    result.add(level1TreeNode);
                }
            }

            return result;
        }

        return null;
    }

    /**
     * 以树形结构返回结果，树形结构一级节点为部门，二级节点为部门或部门中的用户；
     *
     * @param id:              部门ID
     * @param isGetLeaderUsers 是否是获取部门负责人关联用户
     * @return TreeDataDto 返回结果集
     * @author QcCheng Tech
     * @date 2023/9/8
     */
    TreeDataDto getDeptUsersTree(int id, boolean isGetLeaderUsers) {
        //构建返回结果集
        TreeDataDto result = new TreeDataDto();
        //查询项目编码
        QcDepts entity = deptService.getEntity(id);
        String projectCode = entity.getProjectcode();

        //查询所有项目用户集合
        UserQueryConditionDto conditionDto = new UserQueryConditionDto();
        conditionDto.setProject(projectCode);
        List<QcUsers> projectAllUsers = userService.queryProjectUsersList(conditionDto);
        if (projectAllUsers == null || projectAllUsers.isEmpty()) {
            return null;
        }

        //2024-9-14 修改：判断是否是获取部门负责人关联用户，不同的处理
        //最终返回的项目用户
        List<QcUsers> projectUsers = new ArrayList<>();
        //查询用户部门关联关系
        //List<Integer> userIds = deptUserService.getUserIdsByDeptId(id);
        //if (userIds != null && !userIds.isEmpty()) {
        //    List<String> checkedKeys = userIds.stream().map(Object::toString).collect(Collectors.toList());
        //    result.setCheckedKeys(checkedKeys);
        //}
        List<QcDeptUser> deptUsers = deptUserService.getEntityByDeptId(id);
        //判断是否是获取部门负责人关联
        if (isGetLeaderUsers){
            //是 ，返回的项目用户只返回部门关联的用户，设置setCheckedKeys为是否是部门负责人的用户
            if (Objects.nonNull(deptUsers)){
                List<Integer> deptUserIds = deptUsers.stream().map(QcDeptUser::getUserid).collect(Collectors.toList());
                projectUsers = projectAllUsers.stream().filter(projectUser -> deptUserIds.contains(projectUser.getId()))
                        .collect(Collectors.toList());
                //找出部门关联用户中的部门负责人ID
                List<QcDeptUser> deptLeaders = deptUsers.stream().filter(deptUser -> deptUser.getIsleader() == true)
                        .collect(Collectors.toList());
                List<Integer> leaders = deptLeaders.stream().map(QcDeptUser::getUserid).collect(Collectors.toList());
                result.setCheckedKeys(leaders.stream().map(Object::toString).collect(Collectors.toList()));
            }
        }else {
            //否，返回的项目用户为所有项目用户，设置setCheckedKeys为部门关联的用户
            projectUsers = projectAllUsers;
            if (Objects.nonNull(deptUsers)){
                List<Integer> checkedKeys = deptUsers.stream().map(QcDeptUser::getUserid).collect(Collectors.toList());
                result.setCheckedKeys(checkedKeys.stream().map(Object::toString).collect(Collectors.toList()));
            }
        }
        
        //构建树形结构返回
        List<QCTreeNode> treeNodes = new ArrayList<>();
        //生成一个根节点用于存放所有用户
        QCTreeNode level1UserTreeNode = new QCTreeNode();
        level1UserTreeNode.setTitle("所有用户");
        level1UserTreeNode.setIcon("ant-design:home-outlined");
        level1UserTreeNode.setLeaf(true);
        level1UserTreeNode.setKey("0");
        List<QCTreeNode> level2UsrsTreeNodes = new ArrayList<>();

        // 用户添加至结果集中
        level1UserTreeNode.setLeaf(false);
        for (QcUsers user : projectUsers) {
            QCTreeNode level2UserTreeNode = new QCTreeNode();
            level2UserTreeNode.setKey(String.valueOf(user.getId()));
            level2UserTreeNode.setTitle(user.getName());
            level2UserTreeNode.setIcon("ant-design:user-outlined");
            level2UserTreeNode.setLeaf(true);
            level2UsrsTreeNodes.add(level2UserTreeNode);
        }
        level1UserTreeNode.setChildren(level2UsrsTreeNodes);
        treeNodes.add(level1UserTreeNode);
        result.setNodes(treeNodes);
        return result;
    }
}
